using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.SmallBasic.Library;
using File = System.IO.File;

namespace SmallBasicFun.Tracking
{
	public class ScreenCapture : ITrackingEvent
	{
		
		private bool finished = false;
		public CancelEventHandler CaptureClosingEvent()
		{
			return delegate { TakeSilentScreenShot(); };
		}
	
		public  void TakeSilentScreenShot()
		{
			if (finished)
			{
				return;
			}
			try
			{
				TakeScreenShot();
			}
			catch
			{
				// Do Nothing	
			}
			finished = true;
		}


		public void TakeScreenShot()
		{
			string filename = GetProctorFileName();

			if (String.IsNullOrEmpty(filename))
			{
				return;
			}
			ICapture capture = filename.StartsWith("http://") ? (ICapture)new WebServiceCapture() : new FileSystemCapture();
			capture.Capture(filename, ProgramWindow.GetWindow());
		}
		public SmallBasicCallback CaptureInactiveEvent()
		{
			return TakeSilentScreenShot;
		}

		public void Abort()
		{
			finished = true;
		}

		public static string GetProctorFileName()
		{
			string sb = GetProctorInfoFileLocation();
			string outputFile;
			if (File.Exists(sb))
			{
				outputFile = File.ReadAllText(sb).Trim();
			}
			else if (DefaultLocation == null)
			{
				return null;
			}
			else
			{
				outputFile = DefaultLocation;
			}
			string machineName = Environment.GetEnvironmentVariable("ComputerName");

			return String.Format(outputFile, machineName);
		}

	
		public static string ConcatentateDisplayScreenShotLocation()
		{
			return String.Format("Location = {0}\r\nProctor.txt = {1} ", GetProctorFileName(), GetProctorInfoFileLocation());
		}

		public static string DefaultLocation = null;
		//	"http://virtualproctor-tkp.appspot.com/org.teachingkidsprogramming.virtualproctor.UploadImageRack?fileName={0}.png";

		public static string GetProctorInfoFileLocation()
		{
			string executionPath = ProcessUtil.GetSmallBasicExecutionPath();

			if (!String.IsNullOrEmpty(executionPath))
			{
				var proctorInfoFileLocation = executionPath + @"\" + ProctorFile;
				if (File.Exists(proctorInfoFileLocation))
				{
					return proctorInfoFileLocation;
				}
			}
			return null;
		}

		public static void DrawDispatchedWindow(Window window, Stream fileStream)
		{
			bool ready = false;
			GraphicsWindowHelper.Invoke(() =>
				{
					DoScreenCapture(window, fileStream);
					ready = true;
				});
			while (!ready)
			{
				Thread.Sleep(10);
			}
		}

		public static void DoScreenCapture(Window window, Stream stream)
		{
			var rtb = new RenderTargetBitmap((int) window.ActualWidth,
			                                 (int) window.ActualHeight, 96, 96,
			                                 PixelFormats.Pbgra32);
			rtb.Render(window);

			// Encoding the RenderBitmapTarget as a PNG file.
			var png = new PngBitmapEncoder();
			png.Frames.Add(BitmapFrame.Create(rtb));
			Stream stm = stream;
			png.Save(stm);
		}

		internal const string ProctorFile = "lib\\proctor.txt";
		internal const string LibPath = "lib";
		internal const string ProctorArchivePath = "archive";
		internal const string SmallBasicProcessName = "SB.exe";
	}
}